package com.pos.app.comm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;

/**
 * 日期操作工具类.
 * 
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil
{
	public static final String SYS_FORMAT = "yyyyMMddHHmmss";
	public static final String REQUEST_FORMAT = "yyyyMMddHHmm00";
	public static final String QN_FORMAT = "yyyyMMddHHmmssSSS";
	
	public static final String YYYYMMDD = "%1$tY%1$tm%1$td";
	public static final String YYYYMMDDHHMMSS = "%1$tY%1$tm%1$td%1$tH%1$tM%1$tS";
	public static final String MMDDHHMM = "MM-dd HH:mm";
	
	private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static Date str2Date(String str)
	{
		return str2Date(str, null);
	}

	public static Date str2Date(String str, String format)
	{
		if (str == null || str.length() == 0) { return null; }
		if (format == null || format.length() == 0)
		{
			format = FORMAT;
		}
		Date date = null;
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			date = sdf.parse(str);

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return date;

	}

	public static Calendar str2Calendar(String str)
	{
		return str2Calendar(str, null);

	}

	public static Calendar str2Calendar(String str, String format)
	{

		Date date = str2Date(str, format);
		if (date == null) { return null; }
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		return c;

	}

	public static String date2Str(Calendar c)
	{// yyyy-MM-dd HH:mm:ss
		return date2Str(c, null);
	}

	public static String date2Str(Calendar c, String format)
	{
		if (c == null) { return null; }
		return date2Str(c.getTime(), format);
	}

	public static String date2Str(Date d)
	{// yyyy-MM-dd HH:mm:ss
		return date2Str(d, null);
	}

	public static String date2Str(Date d, String format)
	{// yyyy-MM-dd HH:mm:ss
		if (d == null) { return null; }
		if (format == null || format.length() == 0)
		{
			format = FORMAT;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String s = sdf.format(d);
		return s;
	}

	public static String getCurDateStr()
	{
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
	}

	/**
	 * 获得当前日期的字符串格式
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurDateStr(String format)
	{
		Calendar c = Calendar.getInstance();
		return date2Str(c, format);
	}

	// 格式到秒
	public static String getMillon(long time)
	{

		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);

	}

	// 格式到天
	public static String getDay(long time)
	{

		return new SimpleDateFormat("yyyy-MM-dd").format(time);

	}

	// 格式到毫秒
	public static String getSMillon(long time)
	{

		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);

	}

	public static String timeToString(long number)
	{
		Date date = new Date(number);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);

	}

	public static String timeToString(double number)
	{
		Date date = new Date((long) number);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);

	}

	public static String stringForDate(Date date)
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	public static String dateStringS(long date)
	{
		Date d = new Date(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return format.format(d);
	}

	public static String dateString(long date)
	{
		Date d = new Date(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(d);
	}

	public static String dateString(long date, String sformat)
	{
		Date d = new Date(date);
		SimpleDateFormat format = new SimpleDateFormat(sformat);
		return format.format(d);
	}

	public static String dateStringS(Date date)
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return format.format(date);
	}

	public static String dateString(Date date)
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	public static long dateLong(String time, String format)
	{
		long f = 0;
		SimpleDateFormat temformat = new SimpleDateFormat(format);
		try
		{
			java.util.Date date = temformat.parse(time.trim());
			f = date.getTime();
		} catch (ParseException e)
		{

			e.printStackTrace();
		}
		return f;
	}

	@SuppressLint("SimpleDateFormat")
	public static long StringToLong(String time)
	{
		long f = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			java.util.Date date = format.parse(time.trim());
			f = date.getTime();
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return f;
	}

	public static long stringDToLong(String time)
	{
		long f = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			java.util.Date date = format.parse(time);
			f = date.getTime();
		} catch (ParseException e)
		{

			e.printStackTrace();
		}
		return f;
	}

	public static long dateToFloat(Date date)
	{
		return date.getTime();
	}

	public static long beforeDate(Date date, int min)
	{
		Date before = (Date) date.clone();
		before.setTime(date.getTime() - min * 60 * 1000);
		return before.getTime();
	}

	public static long afterDate(Date date, int min)
	{
		Date before = (Date) date.clone();
		before.setTime(date.getTime() + min * 60 * 1000);
		return before.getTime();
	}

	/**
	 * 天
	 * @param number
	 * @return
	 */
	public static String timeDayToString(double number)
	{
		Date date = new Date((long) number);
		SimpleDateFormat format = new SimpleDateFormat("dd");
		return format.format(date);

	}

	/**
	 * 小时分钟
	 * @param number
	 * @return
	 */
	public static String timeToStringForHour(double number)
	{
		Date date = new Date((long) number);
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		return format.format(date);

	}

	public static String timeToStringForTime(long number)
	{
		Date date = new Date(number);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);

	}

	public static long stringToLongForTime(String date)
	{
		long f = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			f = format.parse(date).getTime();
		} catch (ParseException e)
		{

			e.printStackTrace();
		}
		return f;
	}

	public static long stringToLongForMonth(String time)
	{
		long f = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		try
		{
			java.util.Date date = format.parse(time);
			f = date.getTime();
		} catch (ParseException e)
		{

			e.printStackTrace();
		}
		return f;
	}

	public static long stringToLongForYear(String time)
	{
		long f = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		try
		{
			java.util.Date date = format.parse(time);
			f = date.getTime();
		} catch (ParseException e)
		{

			e.printStackTrace();
		}
		return f;
	}

	public static String timeToStringForTimeForHour(long number)
	{
		Date date = new Date(number);
		SimpleDateFormat format = new SimpleDateFormat("HH");
		return format.format(date);

	}

	public static String timeToStringForTimeForDay(long number)
	{
		Date date = new Date(number);
		SimpleDateFormat format = new SimpleDateFormat("dd");
		return format.format(date);

	}

	public static String timeToStringForTimeForMonth(long number)
	{
		Date date = new Date(number);
		SimpleDateFormat format = new SimpleDateFormat("MM");
		return format.format(date);

	}

	public static String timeToStringForTimeForYear(long number)
	{
		Date date = new Date(number);
		SimpleDateFormat format = new SimpleDateFormat("MM-dd");
		return format.format(date);

	}

	public static long StringToLongByHour(String time)
	{
		long f = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
		try
		{
			java.util.Date date = format.parse(time.trim());
			f = date.getTime();
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return f;
	}

	public static String stringToTime(String dateString)
	{

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
		Date date = null;
		SimpleDateFormat df1 = null;
		// String dateString = "20071128175545";
		try
		{
			date = df.parse(dateString);
			df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// //System.out.println(df1.format(date));

		} catch (Exception ex)
		{
			//System.out.println(ex.getMessage());
		}
		return df1.format(date);

	}
	
	public static String stringFormat(Date date, String formatstr)
	{
		String str = "";
		Calendar calendar;
		if (date != null)
		{
			calendar = Calendar.getInstance();
			calendar.setTime(date);
			str = String.format(formatstr, new Object[] { calendar });
		}
		return str;
	}

	public static String stringFormat(String strdate)
	{
		if(strdate != null && !strdate.equals(""))
		{
			Date date = new Date(Long.parseLong(strdate));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf.format(date);
		}
		return "";
	}

	public static String formatDateTime(long time)
	{
		if (0 == time)
		{
			return "";
		}
		SimpleDateFormat mDateFormat = new SimpleDateFormat(MMDDHHMM);
		return mDateFormat.format(new Date(time));
	}
	
	/**
	 * 获取当前月第一天
	 * @return
	 */
	public static String getDateMonthStart()
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        return format.format(c.getTime());
	}
	/**
	 * 获取当前月最后一天
	 * @return
	 */
	public static String getDateMonthEed()
	{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		//获取当前月最后一天
        Calendar c = Calendar.getInstance();    
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
		return format.format(c.getTime());
	}

	public static void main(String[] args)
	{
		//String time = "20110405031803";
		//System.out.println(TimeUtil.StringToTime(time));
	}
}
